package views;

import java.util.ListIterator;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.part.ViewPart;

import views.editingSupports.PropertyNameEditingSupport;
import views.editingSupports.PropertyValueEditingSupport;
import control.ComponentProvider;
import core.Coordinate;
import core.Property;
import factory.ComponentInterface;
import factory.components.Building;
import factory.components.Fence;
import factory.components.Gate;
import factory.components.QuaryCrane;
import factory.components.RailMountedGantry;
import factory.components.ReachStacker;
import factory.components.RoadTruck;
import factory.components.RubberTiredGantry;
import factory.components.Stack;
import factory.components.StraddleCarrier;
import factory.components.Terminal;
import factory.components.TerminalTruck;
import factory.components.Vessel;

public class PropertiesView extends ViewPart {
	public static final String ID = "JavaRoadEditor.propertiesView";

	public PropertiesView() {
	}

	private TableViewer viewer;

	public void createPartControl(Composite parent) {
		GridLayout layout = new GridLayout(2, false);
		parent.setLayout(layout);

		createViewer(parent);
		Button btOk = new Button(parent, SWT.PUSH);
		btOk.setText("OK");
		btOk.setBounds(200, 35, 50, 25);
		btOk.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
				ComponentInterface selComp = ComponentProvider.INSTANCE.getSelected();
				ComponentInterface tempComp = null;

				if (selComp.getClass().getSimpleName().equals("Building")) {
					tempComp = new Building(selComp.getPosition(), selComp.getSize(), "");
				} else if (selComp.getClass().getSimpleName().equals("Fence")) {
					tempComp = new Fence(selComp.getPosition(), selComp.getSize(), selComp.getOrientation());
				} else if (selComp.getClass().getSimpleName().equals("Gate")) {
					tempComp = new Gate(selComp.getPosition(), selComp.getSize(), selComp.getOrientation());
				} else if (selComp instanceof QuaryCrane) { // igy kene az
															// egesszet...
					tempComp = new QuaryCrane(selComp.getPosition());
				} else if (selComp.getClass().getSimpleName().equals("RailMountedGantry")) {
					tempComp = new RailMountedGantry(selComp.getPosition(), selComp.getSize(), selComp.getOrientation());
				} else if (selComp.getClass().getSimpleName().equals("RailMountedGantry")) {
					tempComp = new RailMountedGantry(selComp.getPosition(), selComp.getSize(), selComp.getOrientation());
				} else if (selComp.getClass().getSimpleName().equals("ReachStacker")) {
					tempComp = new ReachStacker(selComp.getPosition(), selComp.getOrientation());
				} else if (selComp.getClass().getSimpleName().equals("RoadTruck")) {
					tempComp = new RoadTruck(selComp.getPosition(), selComp.getOrientation());
				} else if (selComp.getClass().getSimpleName().equals("RubberTiredGantry")) {
					tempComp = new RubberTiredGantry(selComp.getPosition(), selComp.getSize(), selComp.getOrientation());
				} else if (selComp.getClass().getSimpleName().equals("Stack")) {
					tempComp = new Stack(selComp.getPosition(), selComp.getSize(), selComp.getOrientation());
				} else if (selComp.getClass().getSimpleName().equals("StraddleCarrier")) {
					tempComp = new StraddleCarrier(selComp.getPosition(), selComp.getOrientation());
				} else if (selComp.getClass().getSimpleName().equals("Terminal")) {
					tempComp = new Terminal(selComp.getPosition(), selComp.getSize());
				} else if (selComp.getClass().getSimpleName().equals("TerminalTruck")) {
					tempComp = new TerminalTruck(selComp.getPosition(), selComp.getOrientation());
				} else if (selComp.getClass().getSimpleName().equals("Vessel")) {
					tempComp = new Vessel(selComp.getPosition(), selComp.getSize(), selComp.getOrientation());
				}

				ListIterator<Property> li = ComponentProvider.INSTANCE.getProperties().listIterator();
				li.next();
				tempComp.setPosition(new Coordinate(Integer.parseInt(li.next().getValue()), Integer.parseInt(li.next().getValue())));
				tempComp.setSize(new Coordinate(Integer.parseInt(li.next().getValue()), Integer.parseInt(li.next().getValue())));
				String tmpstring = li.next().getValue();
				if (tmpstring == "UP")
					tempComp.setOrientation(SWT.UP);
				else if (tmpstring == "DOWN")
					tempComp.setOrientation(SWT.DOWN);
				else if (tmpstring == "RIGHT")
					tempComp.setOrientation(SWT.RIGHT);
				else if (tmpstring == "LEFT")
					tempComp.setOrientation(SWT.LEFT);

				if (ComponentProvider.INSTANCE.remove(selComp)) {
					ComponentProvider.INSTANCE.setComponent(tempComp);

					if (!ComponentProvider.INSTANCE.isPlaceble()) {
						ComponentProvider.INSTANCE.setComponent(selComp);
						ComponentProvider.INSTANCE.addComponent();
						ComponentProvider.INSTANCE.setComponent(null);
						ComponentProvider.INSTANCE.setSelectedComponent(selComp);
					} else {
						ComponentProvider.INSTANCE.addComponent();
						ComponentProvider.INSTANCE.setComponent(null);
						ComponentProvider.INSTANCE.setSelectedComponent(tempComp);

					}

					ComponentProvider.INSTANCE.redrawProperties();
					ComponentProvider.INSTANCE.redrawEditor();
				}
			}
		});

	}

	private void createViewer(Composite parent) {
		viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
		ComponentProvider.INSTANCE.setPropView(viewer);
		createColumns(parent, viewer);
		final Table table = viewer.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		viewer.setContentProvider(new ArrayContentProvider());

		viewer.setInput(ComponentProvider.INSTANCE.getProperties());
		viewer.refresh();

		getSite().setSelectionProvider(viewer);
		GridData gridData = new GridData();
		gridData.verticalAlignment = GridData.FILL;
		gridData.horizontalSpan = 2;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		viewer.getControl().setLayoutData(gridData);
	}

	public TableViewer getViewer() {
		return viewer;
	}

	private void createColumns(final Composite parent, final TableViewer viewer) {
		String[] titles = { "Name", "Value" };
		int[] bounds = { 75, 90 };

		TableViewerColumn col = createTableViewerColumn(titles[0], bounds[0], 0);
		col.setLabelProvider(new ColumnLabelProvider() {
			public void update(ViewerCell cell) {
				cell.setText(((Property) cell.getElement()).getName());
			}
		});
		col.setEditingSupport(new PropertyNameEditingSupport(viewer));

		col = createTableViewerColumn(titles[1], bounds[1], 1);
		col.setLabelProvider(new ColumnLabelProvider() {
			public void update(ViewerCell cell) {

				cell.setText(((Property) cell.getElement()).getValue());
			}
		});
		col.setEditingSupport(new PropertyValueEditingSupport(viewer));

	}

	private TableViewerColumn createTableViewerColumn(String title, int bound, final int colNumber) {
		final TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
		final TableColumn column = viewerColumn.getColumn();
		column.setText(title);
		column.setWidth(bound);
		column.setResizable(true);
		column.setMoveable(true);
		column.addSelectionListener(getSelectionAdapter(column, colNumber));
		return viewerColumn;

	}

	private SelectionAdapter getSelectionAdapter(final TableColumn column, final int index) {
		SelectionAdapter selectionAdapter = new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				int dir = viewer.getTable().getSortDirection();
				if (viewer.getTable().getSortColumn() == column) {
					dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
				} else {

					dir = SWT.DOWN;
				}
				viewer.getTable().setSortDirection(dir);
				viewer.getTable().setSortColumn(column);
				viewer.refresh();
			}
		};
		return selectionAdapter;
	}

	public void setFocus() {
		viewer.getControl().setFocus();
	}

}
